home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl2 / examples / rasterops / blendrgb.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  4.2 KB  |  172 lines

  1. /*
  2.  * Copyright 1996, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18.  
  19. /* blendimg.c
  20.  *    Demonstrates how to use the GL_EXT_blend_color extensions
  21.  *    to blend two images that do not have an alpha channel.
  22.  *
  23.  *    Up Arrow        - increase blend factor
  24.  *    Down Arrow        - decrease blend factor
  25.  *    Escape key        - exit the program
  26.  */
  27. #include <GL/gl.h>
  28. #include <GL/glu.h>
  29. #include <GL/glut.h>
  30.  
  31. #include <math.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include "rgbImageFile.h"    /* should be found in ../../include */
  35.  
  36. /*  Function Prototypes  */
  37.  
  38. GLvoid  initgfx( GLvoid );
  39. GLvoid  keyboard( GLubyte, GLint, GLint );
  40. GLvoid  specialkeys( GLint, GLint, GLint );
  41. GLvoid  drawScene( GLvoid );
  42. GLvoid  reshape( GLsizei, GLsizei );
  43.  
  44. GLvoid printHelp( char *progname );
  45.  
  46. /* Global Definitions */
  47.  
  48. #define KEY_ESC    27    /* ascii value for the escape key */
  49.  
  50. /* Global Variables */
  51.  
  52. static GLfloat        blend_factor = 0.5;
  53.  
  54. static unsigned int    *image1, *image2;
  55. static int         imgwidth1, imgheight1, imgwidth2, imgheight2;
  56.  
  57. void
  58. main ( int argc, char *argv[])
  59. {
  60.     GLsizei     width, height;
  61.     char         *imgfile[2];
  62.     char        *defaultImage[] = { "aztecNM.rgb", "bricks.rgb" };
  63.                  
  64.  
  65.     glutInit( &argc,  argv );
  66.  
  67.     if (argc < 3) {
  68.         imgfile[0] = ( argc > 1 ? argv[1] : defaultImage[0]);
  69.         imgfile[1] = ( argc > 2 ? argv[2] : defaultImage[1]);
  70.     } else {
  71.         imgfile[0] = argv[1];
  72.         imgfile[1] = argv[2];
  73.     }
  74.  
  75.     image1 = rgbReadImageFile(imgfile[0], &imgwidth1, &imgheight1);
  76.     image2 = rgbReadImageFile(imgfile[1], &imgwidth2, &imgheight2);
  77.  
  78.     width = ( imgwidth1 > imgwidth2 ? imgwidth1 : imgwidth2 );
  79.     height = ( imgheight1 > imgheight2 ? imgheight1 : imgheight2 );
  80.  
  81.     glutInitWindowPosition( 100, 100 );
  82.     glutInitWindowSize( width, height );
  83.     glutInitDisplayMode( GLUT_RGBA | GLUT_SINGLE );
  84.     glutCreateWindow( argv[0] );
  85.  
  86.     initgfx();
  87.  
  88.     glutKeyboardFunc( keyboard );
  89.     glutSpecialFunc( specialkeys );
  90.     glutReshapeFunc( reshape );
  91.     glutDisplayFunc( drawScene ); 
  92.  
  93.     printHelp( argv[0] );
  94.  
  95.     glutMainLoop();
  96. }
  97.  
  98. GLvoid
  99. printHelp( char *progname )
  100. {
  101.     fprintf(stdout, "\n%s, a program to blend two RGB images\n\n"
  102.         "UP Arrow         - increase blend factor\n" 
  103.         "DOWN Arrow         - decrease blend factor\n"
  104.         "Escape key        - exit the program\n\n",
  105.         progname);
  106.     fprintf(stdout, "blend_factor = %4.2f\n", blend_factor );
  107. }
  108.  
  109. GLvoid
  110. initgfx( void )
  111. {
  112.     glClearColor( 0.0, 0.0, 0.0, 1.0 );
  113. }
  114.  
  115. GLvoid 
  116. specialkeys( GLint key, GLint x, GLint y )
  117. {
  118.     switch (key) {
  119.     case GLUT_KEY_UP:    /* increase blend factor */
  120.         if (blend_factor < 1.0) blend_factor += 0.1;
  121.         break;
  122.     case GLUT_KEY_DOWN:    /* decrease blend factor */
  123.         if (blend_factor > 0.0) blend_factor -= 0.1;
  124.         break;
  125.     }
  126.     printf( "blend_factor = %4.2f\n", blend_factor );
  127.     glutPostRedisplay();
  128. }
  129.  
  130. GLvoid 
  131. keyboard( GLubyte key, GLint x, GLint y )
  132. {
  133.     switch (key) {
  134.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  135.         exit(0);
  136.     }
  137. }
  138.  
  139. GLvoid
  140. reshape( GLsizei width, GLsizei height )
  141. {
  142.     glViewport( 0, 0, width, height );
  143.  
  144.     glMatrixMode( GL_PROJECTION );
  145.     glLoadIdentity();
  146.     gluOrtho2D( 0.0, (GLdouble) width, 0.0, (GLdouble) height );
  147.     glMatrixMode( GL_MODELVIEW );
  148.     glLoadIdentity();
  149.     glTranslatef( 0.375, 0.375, 0.0 );
  150. }
  151.  
  152. GLvoid
  153. drawScene( GLvoid )
  154. {
  155.     glClear( GL_COLOR_BUFFER_BIT );
  156.  
  157.     /* draw first image without blending */
  158.     glDisable( GL_BLEND );
  159.     glRasterPos2i( 0,  0 );
  160.     glDrawPixels( imgwidth1, imgheight1, GL_RGBA, GL_UNSIGNED_BYTE, 
  161.             image1 );
  162.  
  163.     /* draw second image with constant alpha */
  164.     glEnable( GL_BLEND );
  165.     glBlendColorEXT( 0, 0, 0, blend_factor );
  166.     glBlendFunc( GL_CONSTANT_ALPHA_EXT, GL_ONE_MINUS_CONSTANT_ALPHA_EXT );
  167.     glDrawPixels( imgwidth2, imgheight2, GL_RGBA, GL_UNSIGNED_BYTE, 
  168.             image2 );
  169.  
  170.     glFlush();
  171. }
  172.